/**
 * Created by Administrator on 2017/7/20.
 */
(function() {
    "use strict";
    var app = angular.module("angularApp");
    app.controller('schoolCtrl', function ($scope, $http,$uibModal,fac) {
        $scope.search = {};
        $scope.pageModel = {};
        $scope.find = function(pageNo){
            $.extend($scope.search,{currentPage:pageNo||$scope.pageModel.currentPage||1,pageSize:$scope.pageModel.pageSize||10});
            fac.getPageResult("../school/list",$scope.search,function(data){
                $scope.pageModel = data;
            });
        };
        $scope.find();
        //删除
        $scope.del = function(item){
            confirm("确认删除此学校吗？",function(){
                $http.get("../school/del?ids="+item.id).success(function(resp){
                    if(resp.code === 0){
                        $scope.find();
                    }else{
                        alert(resp.msg);
                    }
                })
            });
        }

        //添加与保存instance
        $scope.showVideo = function(item){
            var copy = angular.extend({},item);
            var modal = $uibModal.open({
                animation: false,
                size:'',
                templateUrl: 'view/school/video.modal.html',
                controller: 'videoModalCtrl'
                ,resolve: {item: copy}
            });
            modal.result.then(function () {
            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        };


        //添加与保存instance
        $scope.edit = function(item){
            var copy = angular.extend({},item);
            var modal = $uibModal.open({
                animation: false,
                size:'',
                templateUrl: 'view/school/school.modal.html',
                controller: 'schoolModalCtrl'
                ,resolve: {item: copy}
            });
            modal.result.then(function () {
                $scope.find();
            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        };

        //添加与保存instance
        $scope.editPolicy = function(item){
            var copy = angular.extend({},item);
            var modal = $uibModal.open({
                animation: false,
                size:'',
                templateUrl: 'view/school/schoolPolicy.modal.html',
                controller: 'policyModalCtrl'
                ,resolve: {item: copy}
            });
            modal.result.then(function () {
                $scope.find();
            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        };


        $scope.downloadSchools = function(){
            confirm("确认导出学校吗?",function(){
                try{
                    var elemIF = document.createElement("iframe");
                    elemIF.src = "/school/downloadSchools";
                    elemIF.style.display = "none";
                    document.body.appendChild(elemIF);
                }catch(e){

                }
            })
        }

    });

    app.controller('schoolModalCtrl', function($scope,$rootScope,$http,$uibModalInstance,$q,fac,item) {

        $scope.item = item;
        if(item.id){
            $http.get("/school/"+item.id).success(resp=>{
                $.extend(item,resp.data);
            })
        }
        $scope.save = function (form) {
            form.$setSubmitted(true);
            if (!form.$valid) {
                return;
            }
            $http.post("../school/save", item).success(function (resp, status, headers, config) {
                if (resp.code === 0) {
                    $uibModalInstance.close();
                } else {
                    alert(resp.msg);
                }
            })
        }
    });

    app.controller('policyModalCtrl', function($scope,$rootScope,$http,$uibModalInstance,$q,fac,item) {

        $scope.item = item;

        $scope.save = function (form) {
            form.$setSubmitted(true);
            if (!form.$valid) {
                return;
            }
            $http.post("../school/updateSchoolPolicy", item).success(function (resp, status, headers, config) {
                if (resp.code === 0) {
                    $uibModalInstance.close();
                } else {
                    alert(resp.msg);
                }
            })
        }
    });

    app.controller('videoModalCtrl', function($scope,$rootScope,item,$uibModalInstance) {
        let videoServerUrl = $rootScope.videoServerUrl.find(n=>n.id == 1).val;
        $scope.item = item;
        var videoObject = {
            container: '#video', //容器的ID或className
            variable: 'player', //播放函数名称
            //loop: true, //播放结束是否循环播放
            autoplay: true,//是否自动播放
            poster: 'material/poster.jpg', //封面图片
            preview: { //预览图片
                file: ['material/mydream_en1800_1010_01.png', 'material/mydream_en1800_1010_02.png'],
                scale: 2
            },
            //flashplayer:true,
            //live:true,
            //debug:true,
            /*video:[
                ['http://img.ksbbs.com/asset/Mon_1703/05cacb4e02f9d9e.mp4', 'video/mp4', '中文标清', 0],
                ['http://img.ksbbs.com/asset/Mon_1703/d0897b4e9ddd9a5.mp4', 'video/mp4', '中文高清', 0],
                ['http://img.ksbbs.com/asset/Mon_1703/eb048d7839442d0.mp4', 'video/mp4', '英文高清', 0],
                ['http://img.ksbbs.com/asset/Mon_1703/d30e02a5626c066.mp4', 'video/mp4', '英文超清', 0]
            ]*/
            video:[
                [videoServerUrl+item.videoUrl, 'video/mp4', '中文标清', 0]
                //,['http://web.jinbanggaokao.cn/upload/video/阿里阿里.mp4', 'video/mp4', '中文标清', 0]
            ]
        };
        debugger;
        $uibModalInstance.rendered.then(function(){
                var player = new ckplayer(videoObject)
        })

    });

})();